
//依赖
import { useRouter } from 'vue-router'
import {  reactive, watch } from 'vue'
import { filterTimer, filterTab } from '@/utils/util'
// vuex
import { tabParams, upDataDetails, updataPagin } from '@/store/tabParams'
import { ElMessage } from 'element-plus'
import Model from '@/http/api'
const api = new Model()

function handleDataList(){
  const router = useRouter()
   //数据列表
   const dataList = reactive({
    data: [],
  })
  //定义入参
  let options = reactive({
    page: tabParams.page,
    limit: tabParams.limit,
    tab: tabParams.tab,
  })
  //监听数据变化
  watch(tabParams, (newVal) => {
    options = reactive({
      page: tabParams.page,
      limit: tabParams.limit,
      tab: tabParams.tab,
    })
    getDataList()
  })
  //获取数据
  function getDataList() {
    api.getTip(options).then((res) => {
      dataList.data = res.data.data
      filterData(dataList.data)
      console.log(dataList)
      if (res.status === 200 && res.data.data.length) {
        ElMessage.success('获取成功')
      }
    })
  }
  //初始化请求数据列表
   getDataList()
  //格式化时间和tab栏目
  function filterData(params) {
    params.forEach((val, idx) => {
      val.last_reply_at = filterTimer(val.last_reply_at)
      val.tab = filterTab(val.tab, idx)
    })
  }
  //点击跳转详情
  function goDetails(val) {
    //更新数据
    upDataDetails({
      details: val,
    })
    const detailsObj = JSON.stringify(val)
    sessionStorage.setItem("detailsObj",detailsObj)
    //页码控制隐藏
    updataPagin({
      value: false
    })
    router.push({
      name: 'Details',
      params:{
        id: val.id
      }
    })
    
  }
  return {
    goDetails,
    getDataList,
    dataList,
  }
}
export {
  handleDataList
}